package com.ljx.blog.user.controller;

import cn.hutool.json.JSONUtil;
import com.ljx.blog.common.domain.vo.resp.ApiResult;
import com.ljx.blog.common.domain.vo.resp.PageResp;
import com.ljx.blog.security.annotation.ApiPermission;
import com.ljx.blog.security.common.PermissionConstant;
import com.ljx.blog.security.common.UserContext;
import com.ljx.blog.user.domain.dto.UserDto;
import com.ljx.blog.user.domain.entity.User;
import com.ljx.blog.user.domain.vo.req.AuthReq;
import com.ljx.blog.user.domain.vo.req.UserPageReq;
import com.ljx.blog.user.domain.vo.req.UserSubmitReq;
import com.ljx.blog.user.domain.vo.resp.LoginResp;
import com.ljx.blog.user.domain.vo.resp.UserResp;
import com.ljx.blog.user.domain.vo.resp.UserRoleResp;
import com.ljx.blog.user.enums.AuthTypeEnum;
import com.ljx.blog.user.service.TenantService;
import com.ljx.blog.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-10-12  15:16
 */
@Api(tags = "用户接口")
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TenantService tenantService;

    @ApiOperation("登录")
    @ResponseBody
    @PostMapping("/login")
    public ApiResult<LoginResp> login(@RequestBody AuthReq authReq){
        return ApiResult.success(userService.login(authReq));
    }

    @ApiOperation("注册")
    @ResponseBody
    @PostMapping("/register")
    public ApiResult<UserResp> register(@RequestBody AuthReq authReq){
        return ApiResult.success(userService.register(authReq));
    }

    @ApiOperation("获取用户信息（根据ID）")
    @ResponseBody
    @GetMapping("/info/{id}")
    public ApiResult<UserResp> getInfoById(@PathVariable("id") Long userId){
        return ApiResult.success(userService.getInfo(userId));
    }

    @ApiOperation("获取用户信息")
    @ResponseBody
    @PostMapping
    public ApiResult<UserResp> getInfo(@RequestBody UserDto userDto){
        userService.checkPerAndRoleAndMenu(userDto);
        return ApiResult.success(userService.getInfo(UserContext.getUser().getId()));
    }

    @ApiOperation("微信登录回调")
    @GetMapping("/wxLogin")
    public String wxLogin(String code, String state) throws UnsupportedEncodingException {
        AuthReq authReq = new AuthReq();
        authReq.setAuthType(AuthTypeEnum.WX.getKey());
        authReq.setCode(code);
        authReq.setWxState(state);
        LoginResp login = userService.login(authReq);

        return "redirect:http://localhost:11000/?token=" + URLEncoder.encode(login.getToken(), "UTF-8")
                + "&userInfo=" + URLEncoder.encode(JSONUtil.toJsonStr(login.getUserInfo()), "UTF-8");
    }

    @ApiPermission(permissions = {PermissionConstant.USER_LIST})
    @ApiOperation("获取租户的用户列表")
    @ResponseBody
    @GetMapping("/page")
    public ApiResult<PageResp<User>> getUserPage(UserPageReq req){
        return ApiResult.success(tenantService.getUserPage(req));
    }

    @ApiPermission(permissions = {PermissionConstant.USER_NEW_EDIT})
    @ApiOperation("修改用户信息")
    @ResponseBody
    @PostMapping("/submit")
    public ApiResult<UserResp> submit(@RequestBody UserSubmitReq req){
        return ApiResult.success(userService.submit(req));
    }

    @ApiPermission(permissions = {PermissionConstant.USER_LIST})
    @ApiOperation("获取用户角色")
    @ResponseBody
    @GetMapping("/role/{userId}")
    public ApiResult<List<UserRoleResp>> getUserRole(@PathVariable("userId") Long userId){
        return ApiResult.success(userService.getUserRole(userId));
    }

}
